import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {QueryGoodsResponse} from "../apis/GoodsApi";
import {Page, Pageable} from "../utils/Page";
import {ItemOf} from "../types/EnumArray";
import {OffsetDateTime} from "@js-joda/core";

export interface CreateMarketReturnOrderRequest{
    note?:string
}

export function createMarketReturnOrder(marketId: number,request:CreateMarketReturnOrderRequest) {
    return fetcher.post<number>(`markets/${marketId}/returnOrders`)
}

export function getMarketReturnOrderDetail(id: number) {
    return fetcher.get<MarketReturnOrderDetail>(`markets/returnOrders/${id}`, {
        includeItems: true
    })
}

export function getMarketReturnOrderDetailWithoutItems(id: number) {
    return fetcher.get<MarketReturnOrderDetail>(`markets/returnOrders/${id}`)
}

export interface MarketReturnOrderDetail {
    id: number
    createdDateTime: OffsetDateTime
    createdBy: NumberIdAndName
    market: NumberIdAndName
    items: Array<MarketReturnOrderItem>
    submittedDateTime?: OffsetDateTime
    validatedDateTime?: OffsetDateTime
}

export interface MarketReturnOrderItem extends QueryGoodsResponse {
    amountInOrder: number
}

export interface AddMarketReturnOrderItemRequest {
    amount: number
    goodsId: number
}

export function addMarketReturnOrderItem(id: number, addMarketReturnOrderItemRequest: AddMarketReturnOrderItemRequest) {
    return fetcher.post(`markets/returnOrders/${id}/items`, {}, addMarketReturnOrderItemRequest)
}

export function deleteMarketReturnOrderItem(orderId: number, goodsId: number) {
    return fetcher.delete(`markets/returnOrders/${orderId}/items/${goodsId}`)
}

export interface UpdateMarketReturnOrderItemAmountRequest {
    amount: number
}

export function updateMarketReturnOrderItemAmount(orderId: number, goodsId: number, updateMarketReturnOrderItemAmountRequest: UpdateMarketReturnOrderItemAmountRequest) {
    return fetcher.patch(`markets/returnOrders/${orderId}/items/${goodsId}/amount`, {}, updateMarketReturnOrderItemAmountRequest)
}

export interface QueryMarketReturnOrderRequest {
    marketIds: number[]
    startDate?: string
    endDate?: string
    createdByIds: number[]
    status?:MarketReturnOrderStatus
}

export function queryMarketReturnOrders(queryMarketReturnOrderRequest: QueryMarketReturnOrderRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryMarketReturnOrderResponse>>("markets/returnOrders", {...queryMarketReturnOrderRequest, ...pageable})
}

export interface QueryMarketReturnOrderResponse {
    id: number
    createdBy: NumberIdAndName
    createdDateTime: OffsetDateTime
    lastModifiedDateTime: OffsetDateTime
    goodsKindAmount: number
    market: NumberIdAndName
    status: MarketReturnOrderStatus
}

export type MarketReturnOrderStatus = keyof typeof marketReturnOrderStatusMap

export const marketReturnOrderStatusMap =
    {
        "WAIT_SUBMIT": "待提交",
        "SUBMITTED": "已提交",
        "VALIDATED": "已验收",
    } as const
export const marketReturnOrderStatuses = Object.keys(marketReturnOrderStatusMap) as MarketReturnOrderStatus[]

export function getAllMarketReturnOrderCreatedEmployees() {
    return fetcher.get<Array<NumberIdAndName>>("markets/returnOrders/createdBys", {projection: "summary"})
}

export function getMarketReturnOrderCreatedEmployeeByMarketId(id: number) {
    return fetcher.get<Array<NumberIdAndName>>(`markets/${id}/returnOrders/createdBys`, {projection: "summary"})
}

export function submitMarketReturnOrder(id: number) {
    return fetcher.patch<number>(`markets/returnOrders/${id}/submit`)
}

export function deleteMarketReturnOrder(id:number){
    return fetcher.delete<number>(`markets/returnOrders/${id}`)
}

export function validateMarketReturnOrder(id: number) {
    return fetcher.patch<number>(`markets/returnOrders/${id}/validate`)
}